﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Text;
using Model;
using BLL;

public partial class Spa_quanyi : WeiPage
{
    WeiXinRequestBO wb = new WeiXinRequestBO();
    MembershipCardBO mbo = new MembershipCardBO();
    CardUserBO cbo = new CardUserBO();
    MerchantsBO mer = new MerchantsBO();
    CardToUseBO tbo = new CardToUseBO();
    StoreNameBO sbo = new StoreNameBO();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {

                Session["X"] = "121.467095";
                Session["Y"] = "31.238878";
                if (Request.QueryString["code"] != null)
                {
                    try
                    {
                        string url = string.Format(@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + GetAppid() + "&secret=" + GetSecret() + "&code={0}&grant_type=authorization_code", Request.QueryString["code"].ToString());
                        string token = PostRequest(url);
                        if (token.Contains("7200"))
                        {
                            string[] b = token.Split('\"');
                            Session["FromUserName"] = b[13];
                            Session["ToUserName"] = GetAppid();
                        }
                    }
                    catch (Exception)
                    {

                    }
                }

            }
            catch (Exception)
            {

            }
          
        }

        if ((Request.QueryString["FromUserName"] != null || Session["FromUserName"] != null) && (Request.QueryString["ToUserName"] != null || Session["ToUserName"] != null))
        {
            ul.InnerHtml = "";
            se.InnerHtml = "";
            string user = Request.QueryString["FromUserName"] == null ? Session["FromUserName"].ToString() : Request.QueryString["FromUserName"].ToString();
            string user2 = Request.QueryString["ToUserName"] == null ? Session["ToUserName"].ToString() : Request.QueryString["ToUserName"].ToString();
            Session["FromUserName"] = user;
            Session["ToUserName"] = user2;
            List<MembershipCard> list = new List<MembershipCard>();
            list = mbo.GetModelList("1=1 and  type=1 and cid=" + DropDownList1.SelectedValue);
            //            lbCard2.Text = "";
            foreach (MembershipCard card in list)
            {
                  se.InnerHtml+= string.Format(@"<ul >
     <li><bdo>根据您搜索的关键词找到以下门店</bdo></li></ul>");

                StoreName store = sbo.GetModel(card.CID, card.SID);
                ul.InnerHtml += string.Format(@"<li>
      <em><kbd class='tequan'></kbd>&nbsp;特权</em>
       <span><i class='xiala'></i></span><strong>佰草集汉方SPA({0})</strong>
       <bdo>{1}</bdo>
      </li>
      <ol>
     
     {2}
      </ol>", store.Name, card.Title, card.Explain);
            }

        }
     

        try
        {
            if (!IsPostBack)
            {

                if ((Request.QueryString["FromUserName"] != null || Session["FromUserName"] != null) && (Request.QueryString["ToUserName"] != null || Session["ToUserName"] != null))
                {
                    string user = Request.QueryString["FromUserName"] == null ? Session["FromUserName"].ToString() : Request.QueryString["FromUserName"].ToString();
                    string user2 = Request.QueryString["ToUserName"] == null ? Session["ToUserName"].ToString() : Request.QueryString["ToUserName"].ToString();
                    List<WeiXinRequest> list = wb.GetModelList(string.Format(" Wxevent='location' AND FromUserName='{0}' ORDER BY id DESC ", user));
                    if (list.Count == 0)
                    {
                        Response.Write(@"<script> var markerArr = [{ title: '佰草集汉方SPA', content: '佰草集汉方SPA', point: '121.467095|31.238878', isOpen: 0, icon: { w: 23, h: 25, l: 46, t: 21, x: 9, lb: 12} }
		 ];
        function createMap(){
        var map = new BMap.Map('dituContent');
        var point = new BMap.Point(121.488186,31.249162);
        map.centerAndZoom(point,12);
        window.map = map;
    }</script>");
                        Session["X"] = "121.467095";
                        Session["Y"] = "31.238878";
                    }
                    else
                    {
                        se.InnerHtml = "";
                        string mp = @"<script> var markerArr = [{ title: '你的位置', content: '你的位置', point: '{0}|{1}', isOpen: 0, icon: { w: 23, h: 25, l: 46, t: 21, x: 9, lb: 12} }
		 ];
function createMap(){
        var map = new BMap.Map('dituContent');
        var point = new BMap.Point({0},{1});
        map.centerAndZoom(point,12);
        window.map = map;
    }</script>";
                        mp = mp.Replace("{0}", Session["X"].ToString()).Replace("{1}", Session["Y"].ToString());
                        Response.Write(mp);
                        Session["X"] = list[0].Location_X;
                        Session["Y"] = list[0].Location_Y;
                        WebClient client = new WebClient();
                        string url2 = "http://maps.google.com/maps/api/geocode/xml?latlng=" + list[0].Location_X + "," + list[0].Location_Y + "&language=zh-CN&sensor=false";
                        client.Encoding = Encoding.UTF8;
                        string responseTest = client.DownloadString(url2);
                        if (responseTest.Contains("上海"))
                        {
                            DropDownList1.SelectedValue = "1";
                        }
                        else if (responseTest.Contains("北京"))
                        {
                            DropDownList1.SelectedValue = "2";
                        }
                        else if (responseTest.Contains("苏州"))
                        {
                            DropDownList1.SelectedValue = "3";
                        }
                        else if (responseTest.Contains("常州"))
                        {
                            DropDownList1.SelectedValue = "4";
                        }
                        else if (responseTest.Contains("南阳"))
                        {
                            DropDownList1.SelectedValue = "5";
                        }
                        else
                        {
                            DropDownList1.SelectedValue = "1";
                        }
                        DropDownList1_SelectedIndexChanged(null, null);
                        List<MembershipCard> list1 = new List<MembershipCard>();
                        list1 = mbo.GetModelList("1=1 and  type=1 and cid=" + DropDownList1.SelectedValue);
                        //            lbCard2.Text = "";
                        foreach (MembershipCard card in list1)
                        {
                            se.InnerHtml = string.Format(@"<ul >
     <li><bdo>根据您搜索的关键词找到以下门店</bdo></li></ul>");

                            StoreName store = sbo.GetModel(card.CID, card.SID);
                            ul.InnerHtml += string.Format(@"<li>
      <em><kbd class='tequan'></kbd>&nbsp;特权</em>
       <span><i class='xiala'></i></span><strong>佰草集汉方SPA({0})</strong>
       <bdo>{1}</bdo>
      </li>
      <ol>
     
     {2}
      </ol>", store.Name, card.Title, card.Explain);
                        }



                        // Response.Redirect(string.Format("http://api.map.baidu.com/place/search?query=佰草集汉方SPA&location={0},{1}&radius=1000&region=上海&output=html&src=yourCompanyName|yourAppName", list[0].Location_X, list[0].Location_Y));
                    }
                }
                else
                {
                    Response.Write(@"<script> var markerArr = [{ title: '佰草集汉方SPA', content: '佰草集汉方SPA', point: '121.467095|31.238878', isOpen: 0, icon: { w: 23, h: 25, l: 46, t: 21, x: 9, lb: 12} }
		 ];
function createMap(){
        var map = new BMap.Map('dituContent');//在百度地图容器中创建一个地图
        var point = new BMap.Point(121.488186,31.249162);//定义一个中心点坐标
        map.centerAndZoom(point,12);//设定地图的中心点和坐标并将地图显示在地图容器中
        window.map = map;//将map变量存储在全局
    }</script>");
                }
            }
        }
        catch (Exception)
        {

        }

    }


    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }


}